<template>
<view>
<!--hotmall/packageC/number/number.wxml-->
<view class="top">
   <van-search :value="value" placeholder="搜索" @search="onSearch" focus="false"></van-search>
</view>
<view @tap="huanyipi" class="huan">换一批</view>
<view class="phone">
   <view v-for="(item, index) in phoneList" :key="index" class="phone-one" @tap="phonesFun" :data-index="index">{{item.number}}</view>
</view>
</view>
</template>

<script>
// hotmall/packageC/number/number.js
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      index: 1,
      phoneList: [],
      page: 1,
      ssk: '',
      //输入框内容
      phones: 1,
      num: 1,
      goods_id: '',
      keyword: "",
      hong: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      goods_id: options.goods_id
    });
    this.phone();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    phone: function (keyword) {
      console.log(keyword);

      if (keyword == undefined) {
        keyword = '';
      } else keyword = keyword;

      app.globalData.request({
        url: api.default.goodsacquire_number,
        data: {
          goods_id: this.goods_id,
          page: this.page,
          keyword: keyword
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        success: res => {
          console.log(res);
          this.setData({
            phoneList: res.data
          });

          if (res.data.length == 0) {
            this.setData({
              page: 0
            });
          }
        }
      });
    },
    //换一批
    huanyipi: function () {
      if (this.ssk != null) {
        this.setData({
          keyword: this.ssk,
          page: this.page + 1
        });
        console.log(this.keyword);
        this.phone(this.keyword);
      } else {
        this.setData({
          page: this.page + 1
        });
        this.phone();
      }
    },
    // 点击搜索确认
    onSearch: function (event) {
      console.log(event.detail);
      this.setData({
        ssk: event.detail
      });
      app.globalData.request({
        url: api.default.goodsacquire_number,
        data: {
          goods_id: this.goods_id,
          page: 1,
          keyword: event.detail
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        success: res => {
          console.log(res);
          this.setData({
            phoneList: res.data
          });
        }
      });
    },
    phonesFun: function (e) {
      console.log(e.currentTarget.dataset.index);
      let index = e.currentTarget.dataset.index;
      var number = this.phoneList[index].number;
      console.log(number);

      if (this.num % 2 != 0) {
        this.setData({
          hong: this.red,
          num: this.num + 1
        });
      } else {
        this.setData({
          hong: "#ccc",
          num: this.num + 1
        });
      }

      wx.setStorage({
        key: 'number',
        data: number
      });
      wx.navigateBack({
        delta: 1
      });
    }
  }
};
</script>
<style>
@import "./number.css";
</style>